package com.dou.you.che.web.conf.shiro;

import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * shiro 标签配置
 * @Author 黄国刚【1058118328@qq.com】
 */
@Component
public class ShiroTagConfig implements TemplateMethodModelEx {

    @Override
    public Object exec(List args) throws TemplateModelException {
        //默认只取一个
        if(args.size() != 1){
            throw new TemplateModelException("Wrong arguments");
        }
        String permission = String.valueOf(args.get(0));
        Subject subject = SecurityUtils.getSubject();
        return subject != null && subject.isPermitted(permission);
    }
}
